package com.aaa.controller;

import com.aaa.entity.LayData;
import com.aaa.entity.User;
import com.aaa.service.TuserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("user")
public class TbUserController {
    @Resource
    private TuserService tuserService;

//添加
    //发出的是ajax 请求  返回数据要格式统一
    @PostMapping("addUser")
    @ResponseBody
    public LayData insertUser(HttpServletRequest request , User user){
        String title = request.getParameter("title1");
        System.out.println("title"+title);
        String title1 = request.getParameter("title2");
        System.out.println("title1"+title1);
        user.setTopic(title);
        user.setOr1(title1);
        System.out.println("接受过来的参数是"+user);
        boolean status = tuserService.addUser(user);
        return new LayData(status);
    }
//删除
    @PostMapping("delUser")
    @ResponseBody
    public LayData delUser(HttpServletRequest request, User user){
        String title = request.getParameter("title3");
        Integer i = null;
        if(title!=null){
            i=Integer.valueOf(title);
        }
        user.setId(i);

        System.out.println("接受过来的参数是"+user);

        boolean status = tuserService.delUser(user.getId());
        return new LayData(status);
    }

//修改
    @PostMapping("updUser")
    @ResponseBody
    public LayData updUser(HttpServletRequest request, User user){
        String title = request.getParameter("title4");
        Integer i = null;
        if(title!=null){
            i=Integer.valueOf(title);
        }
        user.setId(i);
        user.setTopic(request.getParameter("title5"));
        user.setOr1(request.getParameter("title6"));
        System.out.println("接受过来的参数是"+user);

        boolean status = tuserService.updateByPrimaryKeySelective(user);
        return new LayData(status);
    }

//查询
    @PostMapping("selectUser")
    @ResponseBody
    public LayData selectUser(HttpServletRequest request, User user){
        String title = request.getParameter("title7");
        System.out.println("接受过来的id参数是"+title);
        Integer i = null;
        if(title!=null){
            i=Integer.valueOf(title);
        }
        user.setId(i);
        System.out.println("接受过来的参数是"+user);

        boolean status = tuserService.selectByPrimaryKey(i);
        System.out.println(status);
        return new LayData(status);
    }
    //全部查询
    @PostMapping("selectAll")
    @ResponseBody
    public List<User> selectAll(HttpServletRequest request){
        String title = request.getParameter("null");

        List<User> status= tuserService.selectAll();
        System.out.println(status);
        return status;
    }
}
